package com.yodel.www.tenhou;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

public class UserArrayAdaptor extends ArrayAdapter<UserInfo> {

	private List<UserInfo> mItems;
	private LayoutInflater mInflater;
	private Context mContext;

	//	private boolean isSanmaFlag = false;
	//	private boolean isHououFlag = false;

	public UserArrayAdaptor(Context context, int textViewResourceId,
			List<UserInfo> items) {

		super(context, textViewResourceId, items);
		LogUtils.d(getClass().getName() + "#" + Thread.currentThread().getStackTrace()[2].getMethodName());
		this.mItems = items;
		this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mContext = context;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LogUtils.d(getClass().getName() + "#" + Thread.currentThread().getStackTrace()[2].getMethodName());

		// ビューを受け取る
		View view = convertView;
		if (view == null) {
			// 受け取ったビューがnullなら新しくビューを生成
			view = mInflater.inflate(R.layout.custom_list, null);
			// 背景画像をセットする
			//view.setBackgroundResource(R.drawable.ic_launcher);
		}
		// 表示すべきデータの取得
		final UserInfo item = (UserInfo) mItems.get(position);

		if (item != null && view != null) {
			//開始時刻を設定
			TextView time = (TextView) view.findViewById(R.id.starttime);
			time.setText(item.getStartTime());

			// スクリーンネームをビューにセット
			TextView screenName = (TextView) view.findViewById(R.id.toptext);
			screenName.setTypeface(Typeface.DEFAULT_BOLD);
			if (screenName != null) {
				screenName.setText(item.getPlayerName());
			}

			//			screenName.setOnClickListener(new OnClickListener() {
			//				public void onClick(View v) {
			//					String url = "http://tenhou.net/0/?wg=" + item.getUrlInfo();
			//					Uri uri = Uri.parse(url);
			//					Intent webIntent = new Intent(Intent.ACTION_VIEW,uri);
			//					webIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			//					mContext.startActivity(webIntent);
			//				}
			//			});

			Button watchButton = (Button) view.findViewById(R.id.watchButton);
			watchButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					String url = "http://tenhou.net/0/?wg=" + item.getUrlInfo();
					Uri uri = Uri.parse(url);
					Intent webIntent = new Intent(Intent.ACTION_VIEW,uri);
					webIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
					mContext.startActivity(webIntent);
				}
			});


			Button favoriteButton = (Button) view.findViewById(R.id.favoriteButton);
			favoriteButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					SharedPreferences settings = mContext.getSharedPreferences("pref_favorite", Context.MODE_PRIVATE);
					SharedPreferences.Editor editor = settings.edit();
					editor.putString("KEY001", item.getPlayerName());
					editor.commit();
				}
			});






			//			// MYDATAという名前のSharedPreference
			//			SharedPreferences settings = mContext.getSharedPreferences("MYDATA", mContext.MODE_PRIVATE);
			//
			//			SharedPreferences.Editor editor = settings.edit();
			//			editor.putString("KEY001", "VALUE001");
			//			editor.putString("KEY002", "VALUE002");
			//			editor.putString("KEY003", "VALUE003");
			//			editor.putString("KEY004", "VALUE004");
			//
			//			editor.commit();
			//
			//			読み込み
			//			// MYDATAという名前のSharedPreference
			//			SharedPreferences settings = mContext.getSharedPreferences("MYDATA", mContext.MODE_PRIVATE);
			//			String key001 = settings.getString("KEY001","");
			//			String key002 = settings.getString("KEY002","");
			//			String key003 = settings.getString("KEY003","");
			//			String key004 = settings.getString("KEY004","");

			// テキストをビューにセット
			TextView text = (TextView)view.findViewById(R.id.bottomtext);
			if (text != null) {
				text.setText((item.getRate().toString()));
			}



			TextView middleText = (TextView)view.findViewById(R.id.middletext);
			final int takuInfo = item.getTakuInfo();

			if (item.isSanmaFlag()) {





//				view = mInflater.inflate(R.layout.custom_list_gone, null);




				if (takuInfo == 185) {
					middleText.setText("三麻 鳳南喰赤速");
				} else if (takuInfo == 57) {
					middleText.setText("三麻 特南喰赤速");
				} else if (takuInfo == 121) {
					middleText.setText("三麻 特南喰赤速");
				}
			} else {
				if (takuInfo == 169){
					middleText.setText("四麻 鳳南喰赤");
				} else if (takuInfo == 225) {
					middleText.setText("四麻 鳳東喰赤速");
				} else if (takuInfo == 41) {
					middleText.setText("四麻 特南");
				} else if (takuInfo == 97) {
					middleText.setText("四麻 特東喰赤速");
				} else if (takuInfo == 33) {
					middleText.setText("四麻 特東喰赤");
				} else {
					middleText.setText(String.valueOf(takuInfo));
				}

			}
		}
		return view;
	}

	/**
	 * mItemsを取得します。
	 * @return mItems
	 */
	public List<UserInfo> getmItems() {
		return mItems;
	}

	/**
	 * mItemsを設定します。
	 * @param mItems mItems
	 */
	public void setmItems(List<UserInfo> mItems) {
		this.mItems = mItems;
	}

	/**
	 * mInflaterを取得します。
	 * @return mInflater
	 */
	public LayoutInflater getmInflater() {
		return mInflater;
	}

	/**
	 * mInflaterを設定します。
	 * @param mInflater mInflater
	 */
	public void setmInflater(LayoutInflater mInflater) {
		this.mInflater = mInflater;
	}

	/**
	 * mContextを取得します。
	 * @return mContext
	 */
	public Context getmContext() {
		return mContext;
	}

	/**
	 * mContextを設定します。
	 * @param mContext mContext
	 */
	public void setmContext(Context mContext) {
		this.mContext = mContext;
	}
}